<?php

namespace app\data\service;


use app\data\model\DataEnterpriseExtension;
use app\data\model\DataPolicyInformation;
use think\admin\Service;

/**
 * 企业服务层
 */
class EnterpriseService extends Service
{
    public function handleExtended($params)
    {
        $extended = $params['extension'] ?? [];
        if (!$extended) {
            return;
        }
        $insertData = [];
        $typeMap    = DataPolicyInformation::$fieldMap;
        foreach ($extended as $key => $val) {
            if (!isset($typeMap[$key])) {
                continue;
            }
            $info = DataEnterpriseExtension::query()->where('enterprise_id', $params['id'])
                ->where('policy_information_type', $typeMap[$key])
                ->whereNull('deleted')
                ->find();
            if ($info) {
                $info->value = $val;
                $info->save();
                continue;
            }
            $insertData[] = [
                'enterprise_id'           => $params['id'],
                'policy_information_type' => $typeMap[$key],
                'value'                   => $val,
                'status'                  => 1,
            ];
        }
        if ($insertData) {
            DataEnterpriseExtension::query()->insertAll($insertData);
        }
    }
}
